package com.baizhiedu.rsm.util;


import com.baizhiedu.rsm.entity.Subject;
import com.baizhiedu.rsm.service.SubjectService;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.InputStream;


/**
 * 解析导入用户的工具类
 * @author yjl
 */
public class ParseExcelForSubject {
	/**
	 * 解析导入用户的方法。getCellValue
	 */
	public static void parseAndImportUser(InputStream inputStream,SubjectService subjectService){
			try {
				// 将流中数据实例化为 HSSFWorkbook。
				HSSFWorkbook wb = new HSSFWorkbook(inputStream);
				// 获取sheet工作簿
				HSSFSheet sheet = wb.getSheetAt(0);
				int hang = 0;
				// 获取表中所有行
				hang = sheet.getPhysicalNumberOfRows();
				
				for (int h = 1; h < hang; h++) {
					// 获得工作表的行(用户)。
					Subject subject = new Subject();
					// 获取当前行数据
					HSSFRow row = sheet.getRow(h);

					String content = GetCellValue.getCellValue(row, 0);
					String sid = GetCellValue.getCellValue(row, 1);

					if(content != null && sid != null){
						subject.setContent(content);
						subject.setSid((int)Double.parseDouble(sid));
					}else {
						subject.setContent("");
						subject.setSid(1);
					}
					subjectService.addSubject(subject);
				}
				inputStream.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		

	
}
